#!/bin/sh

# Start the Chimera radio control software

. /etc/utils.sh

start()
{
  echo 1 >/dev/gpio/mcu_reset/value
  msleep 100
  echo 0 >/dev/gpio/mcu_reset/value
  sleep 1

  upgrade_atmel

  echo "starting glaukusd..."
  babysit 60 glaukusd 2>&1 | logos glaukus &

  echo "starting peer-pinger..."
  babysit 60 peer-pinger 2>&1 | logos peer-pinger &

  echo "starting jsonpoll..."
  babysit 60 jsonpoll -i 60 2>&1 | logos jsonpoll 0 20000000 &

  echo "starting craftui..."
  babysit 60 craftui 2>&1 | logos craftui &
}

stop()
{
  echo "stopping craftui..."
  pkillwait -f craftui

  echo "stopping jsonpoll..."
  pkillwait -f jsonpoll

  echo "stopping peer-pinger..."
  pkillwait -f peer-pinger

  echo "stopping glaukusd..."
  pkill -INT glaukusd           # shut down cleanly
  pkillwait -f glaukusd         # TERM then KILL
}

# only for chimera
is-ptp || exit 0

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|reload)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
